﻿using HCQ2_Common.SQL;
using HCQ2_IDAL;
using HCQ2_Model.VR.Params;
using HCQ2_Model.VRDD.InputDto;
using HCQ2_Model.VRDD.InputDto._0525;
using HCQ2_Model.VRDD.OutputDto._0525;
using HCQ2_Model.VRDD.OutputDto._0730;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VRDDCircleDynamicDAL: IT_VRDDCircleDynamicDAL
    {
        /// <summary>
        ///  获取动态信息列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputDynamicList> GetDynamicList(InputDynamicList param)
        {
            sb?.Clear();
            string du_friend = "d.du_friend";
            if(param.dy_type==0)
                du_friend = "du_friend=0";
            sb.Append($@"SELECT TOP {param.rows} * FROM( SELECT {du_friend},u.*,dd.du_name,dd.hide_name,dy.*,(CASE WHEN ISNULL(rd.read_date,'')='' THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END)AS read_status,ROW_NUMBER() OVER(ORDER BY dy.create_date DESC)AS num FROM ");
            //好友、二度人脉
            if (param.dy_type > 0)
                sb.Append($@" (SELECT dd_id_2 AS dd_id,du_friend=1 FROM dbo.DD_1 WHERE dd_id_1={param.user_id} and isfriend=1 UNION SELECT (CASE WHEN dd_id_1={param.user_id} THEN dd_id_2 ELSE dd_id_1 END)AS dd_id,du_friend=2  FROM dbo.DD_2 WHERE dd_id_1={param.user_id} OR dd_id_2={param.user_id} UNION SELECT {param.user_id} AS dd_id,du_friend=0)d INNER JOIN ");
            sb.Append($" (SELECT user_id,user_name,user_img FROM dbo.T_User ");
            //自己
            if (param.dy_type == 0)
                sb.Append($" WHERE user_id={param.user_id} )u");
            else
                //好友、二度人脉
                sb.Append($@")u ON u.user_id=d.dd_id");
            sb.Append($@" INNER JOIN
            (SELECT du_id,du_name,hide_name FROM dbo.DD_User)dd ON u.user_id=dd.du_id INNER JOIN
            (SELECT dy_source as proType, id AS dy_id,du_id,d_note,CONVERT(varchar(100),create_date,120) AS create_date,gps_x,gps_y,create_url,
            video_url=(SELECT TOP 1 video_url FROM T_VRDDCircleVideoList v WHERE v.d_id=d1.du_id)
            ,star_status=(SELECT (CASE WHEN COUNT(0)>0 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END) FROM dbo.T_VRDDCircleUserStar WHERE du_id={param.user_id} AND d_id=d1.id)
             FROM T_VRDDCircleDynamic d1 WHERE 1=1 ");
            //动态来源：VCircle=度度圈（默认值）；VMeta=元宇宙
            if (!string.IsNullOrEmpty(param.proType))
                sb.Append($@" and dy_source='{param.proType}' ");
            sb.Append($@" )dy ON dd.du_id=dy.du_id LEFT JOIN
             (SELECT id, read_date, d_id, du_id FROM T_VRDDCircleReadDynamic)rd ON rd.d_id = dy.dy_id AND rd.du_id = dy.du_id)dymice WHERE dymice.num >{ (param.page - 1) * param.rows}");
            return SqlHelper.ExecuteList<OutputDynamicList>(sb.ToString());
        }
        /// <summary>
        ///  获取动态点赞列表
        /// </summary>
        /// <param name="d_id">动态id</param>
        /// <returns></returns>
        public List<StarList> GetDynamicStar(int d_id)
        {
            sb?.Clear();
            sb.Append($@"SELECT u.*,d.du_name,s.star_date FROM 
            (SELECT du_id,d_id,CONVERT(varchar(100),star_date,120) AS star_date FROM dbo.T_VRDDCircleUserStar WHERE d_id={d_id})s INNER JOIN 
            (SELECT user_id,user_name,user_img FROM dbo.T_User)u ON s.du_id=u.user_id INNER JOIN
            (SELECT du_id,du_name FROM dbo.DD_User)d ON u.user_id=d.du_id;");
            return SqlHelper.ExecuteList<StarList>(sb.ToString());
        }
        /// <summary>
        ///  获取动态评论列表
        /// </summary>
        /// <param name="d_id">动态id</param>
        /// <returns></returns>
        public List<UserReview> GetDynamicReview(int d_id)
        {
            sb?.Clear();
            sb.Append($@"SELECT u.*,d.du_name,s.id,s.re_note,s.re_date FROM 
            (SELECT id,du_id,d_id,re_note,CONVERT(varchar(100),re_date,120) AS re_date FROM dbo.T_VRDDCircleUserReview WHERE d_id={d_id})s INNER JOIN 
            (SELECT user_id,user_name,user_img FROM dbo.T_User)u ON s.du_id=u.user_id INNER JOIN
            (SELECT du_id,du_name FROM dbo.DD_User)d ON u.user_id=d.du_id;");
            return SqlHelper.ExecuteList<UserReview>(sb.ToString());
        }

        /// <summary>
        ///  获取未读的点赞及评论列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputUnreadDynamicList> GetUnreadDynamicList(DDBaseUser param)
        {
            sb?.Clear();
            sb.Append($@"SELECT u.*,d.du_name,type=1,re_note='',CONVERT(varchar(100),s.star_date,120) AS re_date FROM 
            (SELECT du_id,star_date FROM dbo.T_VRDDCircleUserStar WHERE d_user_id={param.user_id} AND star_status=0)s INNER JOIN 
            (SELECT user_id,user_name FROM dbo.T_User)u ON s.du_id=u.user_id INNER JOIN
            (SELECT du_id,du_name FROM dbo.DD_User)d ON u.user_id=d.du_id
            UNION
            SELECT u.*,d.du_name,type=2,s.re_note,CONVERT(varchar(100),s.re_date,120) AS re_date FROM 
            (SELECT du_id,re_note,re_date FROM dbo.T_VRDDCircleUserReview WHERE d_user_id={param.user_id} AND re_status=0)s INNER JOIN 
            (SELECT user_id,user_name FROM dbo.T_User)u ON s.du_id=u.user_id INNER JOIN
            (SELECT du_id,du_name FROM dbo.DD_User)d ON u.user_id=d.du_id");
            return SqlHelper.ExecuteList<OutputUnreadDynamicList>(sb.ToString());
        }
        /// <summary>
        ///  获取人脉关系
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputRelationShip> GetRelationship(InputRelationShip param)
        {
            sb?.Clear();
            sb.Append($@"SELECT * FROM 
            (SELECT du_id={param.user_id},du_friend=0  UNION
            SELECT du_id,du_friend=1 from DD_User with(nolock) 
            where du_id in(SELECT case when dd_id_1={param.user_id} THEN dd_id_2 else dd_id_1 end from DD_1 with(nolock) where dd_id_1={param.user_id} or dd_id_2={param.user_id})
            and du_id in(select case when dd_id_1={param.id} THEN dd_id_2 else dd_id_1 end from DD_1 with(nolock) WHERE dd_id_1={param.id} or dd_id_2={param.id})
            UNION SELECT du_id={param.id},du_friend=2 ) r INNER JOIN
            (SELECT user_id,user_name,user_img,user_status,user_note FROM dbo.T_User)u ON r.du_id=u.user_id INNER JOIN
            (SELECT du_id,gps_x,gps_y FROM dbo.DD_User)d ON r.du_id=d.du_id;");
            return SqlHelper.ExecuteList<OutputRelationShip>(sb.ToString());
        }
        /// <summary>
        ///  获取我的图片动态列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputMyDynamic> GetMyPicDynamic(BaseEParam param,int user_id)
        {
            sb?.Clear();
            sb.Append($@"SELECT TOP {param.rows} d_id,id,img_url FROM (SELECT d_id,id,img_url,ROW_NUMBER() OVER(ORDER BY create_date DESC)AS num  
              FROM T_VRDDCirclePicList WHERE du_id={user_id})d WHERE d.num>{(param.page - 1) * param.rows} ORDER BY d.num;");
            return SqlHelper.ExecuteList<OutputMyDynamic>(sb.ToString());
        }
    }
}